import { LoadModule_blxf } from "./LoadModule";


let pools = {};

const { ccclass, property } = cc._decorator;

/** 对象池模块 */

@ccclass
export default class NodePool_blxf {

    static get(name: string) {
        let pool = pools[name];
        if (!pool) {
            pool = pools[name] = new cc.NodePool();
        }
        // cc.log(name, 'get pool.size =', pool.size());
        if (pool.size() === 0) {
            return LoadModule_blxf.instance.getInstantiate_blxf(name);
        } else {
            return pool.get();
        }
    }

    static put(name: string, node: cc.Node) {
        let pool = pools[name];
        if (!pool) {
            pool = pools[name] = new cc.NodePool();
        }
        pool.put(node);
        // cc.log(name, 'put pool.size =', pool.size());
    }

    // 预先实例化对象
    static autoLoad(names: string[], counts: number[]) {
        for (let i = 0; i < names.length; i++) {
            let objs = [];
            for (let j = 0; j < counts[i]; j++) {
                objs.push(this.get(names[i]));
            }
            for (let j = 0; j < counts[i]; j++) {
                this.put(names[i], objs[j]);
            }
        }
    }

    // 清除缓存节点
    static clear(name: string) {
        let pool = pools[name];
        if (!pool) return;
        pool.clear();
        // cc.warn('pool.size() ==>', name, pool.size());
    }

    // 动态监测对象池节点个数
    static onCheckNodePool() {
        for (let name in pools) {
            let _pool = pools[name];
            let _size = _pool.size();
            // cc.log('name >>', name, _size);

            if (_size < 3) this.autoLoad([name], [1]);
        }
    }

}
